package com.xiaoyun.vo;

import com.xiaoyun.constant.ErrorCode;
import com.xiaoyun.dao.MerchantsDao;
import com.xiaoyun.entity.Merchants;
import io.swagger.annotations.ApiModel;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

/**
 * 创建商户请求对象
 */
@Data
@AllArgsConstructor
@NoArgsConstructor
@ApiModel
public class CreateMerchantsRequest {

    //商户名称
    private String name;
    private String logoUrl;
    private String businessLicenseUrl;
    private String phone;
    private String address;

    /**
     * 验证请求的有效性
     *
     * @param merchantsDao
     * @return
     */
    public ErrorCode validate(MerchantsDao merchantsDao) {
        if (merchantsDao.findByName(this.name) != null) {
            return ErrorCode.DUPLICATE_NAME;
        }
        if (null == this.logoUrl) {
            return ErrorCode.EMPTY_LOGO;
        }
        if (null == this.businessLicenseUrl) {
            return ErrorCode.EMPTY_BUSINESS_LICENSE;
        }
        if (null == this.address) {
            return ErrorCode.EMPTY_ADDRESS;
        }
        if (null == this.phone) {
            return ErrorCode.ERROR_PHONE;
        }
        return ErrorCode.SUCCESS;
    }

    public Merchants toMerchants() {
        Merchants merchants = new Merchants();
        merchants.setName(name);
        merchants.setLogoUrl(logoUrl);
        merchants.setBusinessLicenseUrl(businessLicenseUrl);
        merchants.setPhone(phone);
        merchants.setAddress(address);
        return merchants;
    }


}
